package p1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-09-16
 * Time: 12:41
 */

//UDP 回显服务器 客户端
public class UDPEchoClient {

    DatagramSocket socket = null;

    private int ServerPort;//服务器的端口号
    private String ServerIP;//服务器的IP地址

    public UDPEchoClient(int port, String IP) throws SocketException {
        socket = new DatagramSocket();
        this.ServerIP = IP;
        this.ServerPort = port;
    }

    //启动客户端
    public void start() throws IOException {
        System.out.println("客户端启动!!!");
        Scanner scanner = new Scanner(System.in);
        //客户端输入请求
        String request = scanner.next();

        //构造数据报

        DatagramPacket RequestPacket = new DatagramPacket(request.getBytes(), request.getBytes().length
                , InetAddress.getByName(this.ServerIP), this.ServerPort);


        //把数据报发送给服务器
        socket.send(RequestPacket);

        //从服务器中读取响应
        DatagramPacket responsePacket = new DatagramPacket(new byte[1024], 1024);
        socket.receive(responsePacket);//

    }


}
